package com.service.impl;

import com.common.exception.db.InsertException;
import com.common.exception.db.UpdateException;
import com.dao.AcDao;
import com.service.AcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 2019/1/8 0008.
 */
@Service
public class AcServiceImpl implements AcService {
    @Autowired
    private AcDao dao;


    @Override
    public int findUserAcStatus(Integer userId) {
        String status = dao.findUserAcStatus(userId);
        if(status == null){
            return 0;
        }else{
            if(status.equals("NO")){
                return -1;
            }else if(status.equals("YES")){
                return 1;
            }else if(status.equals("WAIT")){
                return -2;
            }
        }
        return 0;
    }

    @Override
    @Transactional
    public int insertDentify(String userId, String filePath, String position) {
        int user = Integer.parseInt(userId);
        String status = dao.findUserAcStatus(user);
        if(status == null){
            int count = dao.insertAc(user);
            if(count > 0){
                if (position.equals("id_card_front")) {
                   count =  dao.updateDentify(user, filePath, 0);
                   if(count > 0){
                       return count;
                   }else{
                       throw new UpdateException("修改失败");
                   }
                } else if (position.equals("id_card_back")) {
                    count = dao.updateDentify(user, filePath, 1);
                    if(count > 0){
                        return count;
                    }else{
                        throw new UpdateException("修改失败");
                    }
                } else if (position.equals("student_card")) {
                    count = dao.updateDentify(user, filePath, 2);
                    if(count > 0){
                        return count;
                    }else{
                        throw new UpdateException("修改失败");
                    }
                } else if (position.equals("other")) {
                    count = dao.updateDentify(user, filePath, 3);
                    if(count > 0){
                        return count;
                    }else{
                        throw new UpdateException("修改失败");
                    }
                } else if(position.equals("hand_card")){
                    count = dao.updateDentify(user, filePath, 4);
                    if(count > 0){
                        return count;
                    }else{
                        throw new UpdateException("修改失败");
                    }
                }
            }else{
                throw new InsertException("添加失败");
            }
        }else{
            int success;
            if(status.equals("NO")){
                success = dao.updateAcStatusIsNo(user);
                if(success == 0){
                    throw new UpdateException("修改失败");
                }
            }
            if (position.equals("id_card_front")) {
                success =  dao.updateDentify(user, filePath, 0);
                if(success > 0){
                    return success;
                }else{
                    throw new UpdateException("修改失败");
                }
            } else if (position.equals("id_card_back")) {
                success = dao.updateDentify(user, filePath, 1);
                if(success > 0){
                    return success;
                }else{
                    throw new UpdateException("修改失败");
                }
            } else if (position.equals("student_card")) {
                success = dao.updateDentify(user, filePath, 2);
                if(success > 0){
                    return success;
                }else{
                    throw new UpdateException("修改失败");
                }
            } else if (position.equals("other")) {
                success = dao.updateDentify(user, filePath, 3);
                if(success > 0){
                    return success;
                }else{
                    throw new UpdateException("修改失败");
                }
            } else if(position.equals("hand_card")){
                success =  dao.updateDentify(user, filePath, 4);
                if(success > 0){
                    return success;
                }else{
                    throw new UpdateException("修改失败");
                }
            }
        }
       return 0;
    }
}
